package org.lex.dbq.util;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class DialogUtils {
	public static File chooseDirectory(Shell shell, String msg) {
		DirectoryDialog d = new DirectoryDialog(shell, SWT.OPEN);
		d.setMessage(msg);
		String path = d.open();
		File dir = null;
		if (null != path) {
			dir = new File(path);
		}
		return dir;
	}

	public static File chooseFile(Shell shell, int style, String... extensions) {
		File[] files = chooseFiles(shell, style, extensions);
		return (null == files || 0 == files.length) ? null : files[0];
	}

	public static File[] chooseFiles(Shell shell, int style, String... extensions) {
		FileDialog d = new FileDialog(shell, style);
		d.setFilterExtensions(extensions);
		File dir = null;
		String path = d.open();
		if (null != path) {
			dir = new File(path).getParentFile();
		}

		String[] paths = d.getFileNames();
		File[] files = null;
		if (null != paths) {
			files = new File[paths.length];
			for (int i = 0; i < paths.length; i++) {
				files[i] = new File(dir, paths[i]);
			}
		}
		return files;
	}
}
